<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <!--遍历数组-->
    <h2>人员列表</h2>
    <!--    <input type="text" placeholder="请输入名字" @change="search($event)">-->
    <input type="text" placeholder="请输入名字" v-model:value="keyword">
    <button @click="sortType=1">年龄升序</button>
    <button @click="sortType=2">年龄降序</button>
    <button @click="sortType=0">原顺序</button>
    <ul>
        <li v-for="p in people" :key="p.id">
            {{p.name}}--{{p.age}}岁--{{p.gender}}
            <input type="text">
        </li>
    </ul>
</div>
</body>
<script type="text/javascript">
    Vue.config.productionTip = false;
    // 用watch实现
    /*const vm = new Vue({
        el: '#root',
        data() {
            return {
                keyword: "",
                people0: [
                    {id: '001', name: '马冬梅', age: 18, gender:'女'},
                    {id: '002', name: '周冬雨', age: 19, gender:'女'},
                    {id: '003', name: '周杰伦', age: 20, gender:'男'},
                    {id: '004', name: '温兆伦', age: 21, gender:'男'},
                ],
                people: []
            }
        },
        computed: {},
        methods: {
            /!*search(e){
                let str = e.currentTarget.value;
                let temp = [];
                for (let p of this.people) {
                    if (p.name.includes(str)) {
                        temp.push(p);
                    }
                }
                this.people = temp;
            }*!/
        },
        watch: {
            'keyword': {
                immediate: true,
                handler(valNew, valOld){
                    this.people = this.people0.filter((p) => {
                        return p.name.includes(valNew);
                    });
                }
            }
        }
    });*/

    // 用computed实现
    const vm = new Vue({
        el: '#root',
        data() {
            return {
                keyword: "",
                people0: [
                    {id: '001', name: '马冬梅', age: 18, gender: '女'},
                    {id: '002', name: '周冬雨', age: 19, gender: '女'},
                    {id: '004', name: '温兆伦', age: 21, gender: '男'},
                    {id: '003', name: '周杰伦', age: 20, gender: '男'},
                ],
                sortType: 0, /*0：原顺序；1：升序；2：降序*/
            }
        },
        computed: {
            people() {
                let temp = this.people0.filter((p) => {
                    return p.name.includes(this.keyword);
                });
                switch (this.sortType) {
                    case 0:
                    default:
                        //原顺序
                        return temp;
                    case 1:
                        //升序
                        return temp.sort((p0, p1) => {
                            return p0.age - p1.age;
                        });
                    case 2:
                        //降序
                        return temp.sort((p0, p1) => {
                            return p1.age - p0.age;
                        });
                }
            }
        },
        methods: {
            /*asc(e){
                this.people = this.people.sort((p0, p1) => {
                    return p0.age - p1.age;
                });
            },
            desc(e){
                
            },
            ori(e){
                
            },*/
        },
        watch: {}
    });
</script>
</html>